/**
 * 楼栋管理 API
 */

import { get, post, put, del, upload, download } from '@/utils/request'

/**
 * 获取楼栋列表
 * @param {Object} params { page, pageSize, name }
 * @returns {Promise}
 */
export function getBuildingList(params) {
    return get('/building/list', params)
}

/**
 * 获取楼栋详情
 * @param {String|Number} id 楼栋ID
 * @returns {Promise}
 */
export function getBuildingDetail(id) {
    return get('/building/detail', { id })
}

/**
 * 添加楼栋
 * @param {Object} data 楼栋信息
 * @returns {Promise}
 */
export function addBuilding(data) {
    return post('/building/add', data)
}

/**
 * 更新楼栋信息
 * @param {String|Number} id 楼栋ID
 * @param {Object} data 楼栋信息
 * @returns {Promise}
 */
export function updateBuilding(id, data) {
    return post('/building/update', { ...data, id })
}

/**
 * 删除楼栋
 * @param {String|Number} id 楼栋ID
 * @returns {Promise}
 */
export function deleteBuilding(id) {
    return del(`/building/delete/${id}`)
}

/**
 * 批量删除楼栋
 * @param {Array} ids 楼栋ID数组
 * @returns {Promise}
 */
export function batchDeleteBuildings(ids) {
    return post('/building/batch-delete', ids)
}

/**
 * 导入楼栋数据
 * @param {File} file Excel 文件
 * @param {Function} onProgress 上传进度回调
 * @returns {Promise}
 */
export function importBuildings(file, onProgress) {
    const formData = new FormData()
    formData.append('file', file)
    return upload('/building/import', formData, onProgress)
}

/**
 * 导出楼栋数据
 * @param {Object} params 搜索条件
 * @returns {Promise}
 */
export function exportBuildings(params = {}) {
    const filename = `楼栋列表_${new Date().toISOString().slice(0, 10)}.xlsx`
    return download('/building/export', params, filename)
}

/**
 * 下载楼栋导入模板
 * @returns {Promise}
 */
export function downloadBuildingTemplate() {
    return download('/building/template', {}, '楼栋信息导入模板.xlsx')
}

/**
 * 获取所有楼栋（用于下拉选择）
 * @returns {Promise}
 */
export function getAllBuildings() {
    return get('/building/all')
}

/**
 * 获取楼栋统计信息
 * @returns {Promise}
 */
export function getBuildingStatistics() {
    return get('/building/statistics')
}

